package cn.agiledata.bank.linkbank.impl.hxb;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import Union.UnionException;

import com.tws.CertController;
import cn.agiledata.bank.common.util.IOUtil;
import cn.agiledata.bank.linkbank.exception.LinkBankException;

public class HxbUtils {
    /**
     * 按照华夏初始文件生成密文
     * 
     * @param keys
     *            参与生成的顺序相关的关键字组
     * @param values
     *            所有参数
     * @param initFilePath
     *            初始文件路径
     * @return String 密文
     * @throws LinkBankException
     *             若生成过程中出现错误
     */
    public static String getDigitalSign(Map values,String[] keys,String iniFilePath)
            throws LinkBankException {
       try {
    	   String iniFile  = getInitFilePath(iniFilePath);
		   CertController encrypt = new CertController(iniFile);
		   String srcData = getScrabbleContent(values,keys);
		   return encrypt.encryptAndSignDataToBank(srcData);
       } catch (IOException e) {
           throw LinkBankException.CANNOT_LOAD_CERT;
       } catch (UnionException e) {
           throw LinkBankException.CANNOT_LOAD_CERT;
       } catch (Exception e) {
           throw LinkBankException.getDigitalSignException(e.getMessage());
       } 
    }
    /**
     * 按照民生银行私钥解析密文
     * 
     * @param digitalSign
     *            银行返回的密文
     * @param keys
     *            参与生成的顺序相关的关键字组
     * @param cryptoKey
     *            私钥密码
     * @param certFilePath
     *            私钥文件路径
     * @return 明文解析后的Map
     * @throws LinkBankException
     *             若解析过程中出现错误
     */
    public static Map parseDigitalSign(String digitalSign, String[] keys,
            String cryptoKey, String iniFilePath) throws LinkBankException {
        try {
        	String iniFile  = getInitFilePath(iniFilePath);
        	CertController encrypt = new CertController(iniFile);
        	String content = encrypt.decryptAndVerifySignData(digitalSign);
        	return parseScrabbleContent(content,keys);
        } catch (IOException e) {
            throw LinkBankException.CANNOT_LOAD_CERT;
        } catch (UnionException e) {
            throw LinkBankException.CANNOT_LOAD_CERT;
        } catch (Exception e) {
            throw LinkBankException.getDigitalSignException(e.getMessage());
        }
    }
    /**
     * 得到文件路径
     * @param initFilePath
     * @return
     */
    public static String getInitFilePath(String initFilePath) {
    	return IOUtil.getAbsolutePath(initFilePath);
    }
    /**
     * 得到组织好订单信息
     * @param keys
     * @return
     */
    public static String getScrabbleContent(Map values,String[] keys) {
    	String content = "";
    	for (int i = 0; i < keys.length; i++) {
            content += values.get(keys[i])
                    + HxbNetConstant.REQUEST_SEPARATOR;
        }
        if (content.length() > 0) {
            content = content.substring(0, content.length() - 1);
        }
        return content;
    }
    /**
     * 按照华夏银行生成数字签名的规则解析明文
     * 
     * @param value
     *            明文
     * @param keys
     *            参与校验的顺序相关的关键字组
     * @return 解析后的明文
     */
    private static Map parseScrabbleContent(String content, String[] keys) {
        Map values = new HashMap();
        String[] tempValues = content.split(HxbNetConstant.RESPONSE_SEPARATOR);
        for (int i = 0; i < keys.length; i++) {
            if (i < tempValues.length) {
                values.put(keys[i], tempValues[i]);
            } else {
                values.put(keys[i], "");
            }
        }
        return values;
    }
}
